//
//  HomeViewController.m
//  ZHIFUBAO
//
//  Created by 高新川 on 15/6/16.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "HomeViewController.h"
#import "UIColor+Hex.h"
#import "HomeView.h"
#import "ADScrollView.h"
#import "UIDragButton.h"
#import "TrainViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(buttonChange:) name:@"buttonChanged" object:nil];
    
    self.title = @"支付宝";
    
    // 添加账单按钮
    UIBarButtonItem *bill = [[UIBarButtonItem alloc] initWithTitle:@"账单" style:UIBarButtonItemStylePlain target:self action:@selector(clickedBill)];
    self.navigationItem.rightBarButtonItem = bill;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"支付宝" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self initTopView];
    [self initHomeView];
    [self initScrollView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 初始化带有扫一扫 付款码的视图
- (void)initTopView {
    CGRect wnd = self.view.frame;
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wnd.size.width, wnd.size.width/3+64)];
    top.backgroundColor = [UIColor colorWithHexString:@"36648B" alpha:1.0];
    [self.view addSubview:top];
    
    // 扫一扫按钮
    UIButton *scan = [[UIButton alloc] initWithFrame:CGRectMake(50, 76, 60, 79)];
    [scan setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
    [scan addTarget:self action:@selector(clickedScan) forControlEvents:UIControlEventTouchDown];
    [top addSubview:scan];
    
    // 付款码按钮
    UIButton *pay = [[UIButton alloc] initWithFrame:CGRectMake(210, 76, 60, 79)];
    [pay setImage:[UIImage imageNamed:@"home_pay"] forState:UIControlStateNormal];
    [pay addTarget:self action:@selector(clickedPay) forControlEvents:UIControlEventTouchDown];
    [top addSubview:pay];
}

// 初始化homeView，ps：必须先初始化homeview才能初始化下边的广告投放栏
- (void)initHomeView {CGRect wnd = self.view.frame;
    _imageArray = [[NSMutableArray alloc] initWithObjects:@"HomeIcon2", @"HomeIcon1",
                    @"HomeIcon0", @"HomeIcon3",
                    @"HomeIcon4", @"HomeIcon5",
                    @"HomeIcon6", @"10000003",
                    @"10000008", @"10000033",
                    @"20000119", @"20000123",
                    @"20000135", @"20000153",
                    @"20000155", @"20000168",
                    @"i01", @"i03", @"i02", nil];
    self.homeView = [[HomeView alloc] initWithFrame:CGRectMake(0, 320/3+64, 320, wnd.size.height-221)];
    _homeView.contentSize = CGSizeMake(320, 620);
    _homeView.backgroundColor = [UIColor colorWithHexString:@"EDEDED" alpha:1.0];
    _homeView.tag = 101;
    
    _buttonArray = [[NSMutableArray alloc] initWithCapacity:19];
    for (NSInteger i = 0; i < [self.imageArray count]; i++) {
        UIDragButton *button = [UIDragButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:[_imageArray objectAtIndex:i]] forState:UIControlStateNormal];
        if (i < 12) {
            button.frame = CGRectMake((i%4)*80, (i/4)*100, 80, 100);
        }
        else {
            button.frame = CGRectMake((i-12)%4*80, 420+(i-12)/4*100, 80, 100);
        }
        if (i == 12) {
            [button addTarget:self action:@selector(trainSearch:) forControlEvents:UIControlEventTouchDown];
        }
        button.indexOfArray = i;
        [button initStyle];
        button.buttonArray = _buttonArray;
        [self.buttonArray addObject:button];
        [self.homeView addSubview:button];
    }
    [self.view addSubview:_homeView];
}

// 初始化广告投放栏
- (void)initScrollView {
    self.adScroll = [[ADScrollView alloc] init];
    self.adScroll.frame = CGRectMake(0, 310, 320, 100);
    self.adScroll.delegate = self;
    //self.adScroll.adDelegate = self;
    [self.homeView addSubview:_adScroll];
    
    self.page = [[UIPageControl alloc] init];
    _page.frame = CGRectMake(150, 390, 50, 10);
    _page.numberOfPages = [_adScroll.ads count];
    _page.currentPage = 0;
    _page.currentPageIndicatorTintColor = [UIColor blueColor];
    _page.pageIndicatorTintColor = [UIColor grayColor];
    [_page addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    [self.homeView addSubview:self.page];
}

- (void)pageChanged:(UIPageControl *)page {
    NSLog(@"点击了");
    NSUInteger pageindex = page.currentPage;
    [self.adScroll changeToImage:pageindex];
}

// scrollview 滑动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger dic = [_adScroll scrollToPoint:scrollView.bounds.origin];
    if (1 == dic) {     // 右滑
        _page.currentPage = (_page.currentPage+1)%_page.numberOfPages;
    }
    else if (-1 == dic){  // 左滑
        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger dic = [_adScroll scrollToPoint:scrollView.bounds.origin];
    if (1 == dic) {     // 右滑
        _page.currentPage = (_page.currentPage+1)%_page.numberOfPages;
    }
    else if (-1 == dic){  // 左滑
        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;
    }
}

- (void)scrolTimer:(NSInteger)dictionary {
    if (1 == dictionary) {
        _page.currentPage = (_page.currentPage+_page.numberOfPages+1)%_page.numberOfPages;
    }
    else if(-1 == dictionary) {
        _page.currentPage = (_page.currentPage+_page.numberOfPages-1)%_page.numberOfPages;
    }
    NSLog(@"%@",_adScroll);
}

- (void)clickedBill {
    NSLog(@"账单点击");
}

- (void)clickedScan {
    NSLog(@"扫一扫");
}

-(void)clickedPay {
    NSLog(@"付款码");
}

-(void)trainSearch:(UIDragButton *)button {
    TrainViewController *controll = [[TrainViewController alloc] init];
    controll.title = @"火车查询";
    
    [self.navigationController pushViewController:controll animated:YES];
}

- (void)buttonChange:(NSNotification *)object {
    NSNumber *number = [object object];
    [[self.view viewWithTag:101] bringSubviewToFront:[_buttonArray objectAtIndex:[number integerValue]]];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
